
let departmentaddMudule = (function(){
    let departmentId = null,
        $departmentName = $('#departmentName'),
        $departmentDesc = $('#departmentDesc'),
        $spanName = $('.spanName'),
        $spanDesc = $('.spanDesc'),
        $submit = $('.submit');

        // 表单校验
        function checkName(){
            let val = $departmentName.val().trim();
            if(val.length===0){
                $spanName.html('当前是必填项');
                return false;
            }
            // 预防跨站简本攻击
            let reg = /<script>|<script\/>/g;
            if(reg.test(val)){
                val.replace(reg,' ');
            }
            $spanName.html('');
            return true;
        }
        function checkDesc(){
            let val = $departmentDesc.val().trim();
            if(val.length===0){
                $spanDesc.html('当前是必填项');
                return false;
            }
            // 预防跨站简本攻击
            let reg = /<script>|<script\/>/g;
            if(reg.test(val)){
                val.replace(reg,' ');
            }
            $spanDesc.html('');
            return true;
        }

        //提交用户信息 
        async function submitHandle(){
            if(!checkDesc()||!checkName()) return;
            let params = {
                name:$departmentName.val(),
                desc:$departmentDesc.val()
            };
            if(departmentId){
                let result = await axios.post('/department/update',{
                    name:params.name,
                    desc:params.desc,
                    departmentId
                });
                if(result.code==0){
                    alert('恭喜你，修改成功');
                    $departmentName.val('');
                    $departmentDesc.val('');
                    return;
                }
            }else{
                let result = await axios.post('/department/add',params);
                if(result.code==0){
                    alert('恭喜你，添加成功');
                    $departmentName.val('');
                    $departmentDesc.val('');
                    return;
                }

            }
            alert('小主，当前网络繁忙，请您刷新后重试~~');
        }

        // 从编辑页跳过来=>获取信息至文本框
        async function queryBaseInfo(){
            let result = await axios.get('/department/info',{
                params:{
                    departmentId
                }
            });
            if(result.code==0){
                $departmentName.val(result.data.name);
                $departmentDesc.val(result.data.desc);
                return; 
            }
            alert('当前编辑的部门信息不存在，请查证');
            departmentId = null;
        }
    return {
        init(){
            let params = window.location.href.queryURLParams();
            if(params.hasOwnProperty('departmentId')){
                departmentId = params.departmentId;
                queryBaseInfo();
            }
            $departmentName.blur(checkName);
            $departmentDesc.blur(checkDesc);
            $submit.click(submitHandle);
        }
    }
})();
departmentaddMudule.init();